package com.wimcorp.magic.cost;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;

import com.wimcorp.magic.exception.CostException;

@Embeddable
public class CostHolder {

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@OrderColumn
	private List<Cost> costs = new ArrayList<Cost>();

	public CostHolder() {
	}

	public void payCost() throws CostException{
		for (Cost cost : costs) {
			if(!cost.isPayed()){
			//Disabled this for easier testing
			//cost.payCost();
			}
		}
		//Reset payment.
		for (Cost cost : costs) {
			cost.setPayed(false);
		}
	}

	public void addCost(Cost cost) {
		costs.add(cost);
	}

	public void removeCost(Cost cost) {
		costs.remove(cost);
	}

	public List<Cost> getCosts() {
		return costs;
	}

	public int size() {
		return costs.size();
	}

	public String toString() {
		StringBuilder costSB = new StringBuilder();
		for (Cost cost : costs) {
			costSB.append(cost);
		}
		return costSB.toString();
	}
}
